local skynet = require "skynet"
local filelog = require "filelog"
local msghelper = require "tablehelper"
local timer = require "timer"
local tabletool = require"tabletool"
local timetool = require "timetool"
require "cardvalue"
require "pszenum"
local tablelogic 	-- = require "tablelogic"
local seatlogic 	-- = require "seatlogic"
local gamelogic 	-- = require "gamelogic"

local server, table_data
local TableRequest = {}

function TableRequest.init(base)
    server = base
    table_data = server.table_data
	tablelogic = server.logic.tablelogic
	seatlogic = server.logic.seatlogic
	gamelogic = server.logic.gamelogic
end

function TableRequest.disconnect(request)
	local result
	local seat = tablelogic.get_seat_by_rid(table_data, request.rid)
	if seat == nil then
		return true
	end	
	tablelogic.disconnect(table_data, request, seat)
    return true
end

function TableRequest.entertable(request)
	local responsemsg = {}

	if server.will_kill then
		responsemsg.retcode = EErrCode.ERR_INVALID_REQUEST
		responsemsg.retstr = "服务器维护中"
		return responsemsg
	end

	if table_data.conf.room_type == ERoomType.ROOM_TYPE_DIAMOND then
		--判断玩家金币是否够最小携带
		if not request.money.diamond or request.money.diamond < table_data.conf.min_carry_diamond then
			responsemsg.retcode = EErrCode.ERR_NOT_ENOUGH_DIAMOND
			responsemsg.retstr = "您的余额不足，不能继续游戏！"
			return responsemsg
		end
	elseif table_data.conf.room_type == ERoomType.ROOM_TYPE_COIN then
		--判断玩家金币是否够最小携带
		-- if not request.money.coin or request.money.coin < table_data.conf.min_carry_diamond then
		if not request.money.coin or (request.money.coin < 10) or (request.money.coin + request.money.diamond < table_data.conf.min_carry_diamond) then
			responsemsg.retcode = EErrCode.ERR_NOT_ENOUGH_COIN
			responsemsg.retstr = "您的金币不足，不能继续游戏！"
			return responsemsg
		end
	elseif table_data.conf.room_type == ERoomType.ROOM_TYPE_ZJ_FRIEND then
		if table_data.enterclose then
			responsemsg.retcode = EErrCode.ERR_INVALID_REQUEST
			responsemsg.retstr = "游戏已经开始，不能进入！"
			return responsemsg
		end
		if table_data.cur_games_times >= table_data.conf.game_times then
			responsemsg.retcode = EErrCode.ERR_GAME_TIMES_OVER
			responsemsg.retstr = "局数已用完，请重新创建房间！"
			return responsemsg
		end
	elseif table_data.conf.room_type == ERoomType.ROOM_TYPE_ZJ_DIAMOND then
		--判断玩家金币是否够最小携带
		if not request.money.diamond or request.money.diamond < table_data.conf.min_carry_diamond then
			responsemsg.retcode = EErrCode.ERR_NOT_ENOUGH_DIAMOND
			responsemsg.retstr = "您的余额不足，不能继续游戏！"
			return responsemsg
		end
		if table_data.cur_games_times >= table_data.conf.game_times then
			responsemsg.retcode = EErrCode.ERR_GAME_TIMES_OVER
			responsemsg.retstr = "局数已用完，请重新创建房间！"
			return responsemsg
		end
	end
	
	if tablelogic.is_full(table_data) then
		responsemsg.retcode = EErrCode.ERR_TABLE_FULL
		responsemsg.retstr = "房间人数已满！"
		return responsemsg
	end

	local seat = tablelogic.get_seat_by_rid(table_data, request.rid)
	if seat then
		seat.playerinfo.rolename = request.playerinfo.rolename
		seat.playerinfo.logo = request.playerinfo.logo
		seat.playerinfo.sex = request.playerinfo.sex
	end
	responsemsg.gameinfo = {}
	msghelper:copy_table_gameinfo(responsemsg.gameinfo)
	tablelogic.entertable(table_data, request, seat)
	
    return responsemsg
end

function TableRequest.reentertable(request)
	-- filelog.debug("====>>>", request.money)
	local responsemsg = {
		-- retcode = EErrCode.ERR_SUCCESS, 
	}
	local seat = tablelogic.get_seat_by_rid(table_data, request.rid)
	if seat ~= nil then
		seat.playerinfo.rolename = request.playerinfo.rolename
		seat.playerinfo.logo = request.playerinfo.logo
		seat.playerinfo.sex = request.playerinfo.sex
		seat.playerinfo.fangka = request.money.fangka
		seat.getfangka = request.money.fangka
		if table_data.conf.room_type == ERoomType.ROOM_TYPE_DIAMOND or table_data.conf.room_type == ERoomType.ROOM_TYPE_ZJ_DIAMOND then
			if not table_data.balanced and request.money.diamond ~= seat.diamond_base then
				filelog.sys_error("reentertable: seat info =>", request.money, seat.diamond_base)
				local diff = seat.diamond_base - request.money.diamond
				seat.diamond = seat.diamond - diff
				seat.getdiamond = seat.getdiamond - diff
				seat.diamond_base = request.money.diamond
				seat.playerinfo.diamond = request.money.diamond
				seat.playerinfo.coin = request.money.coin
			end
		elseif table_data.conf.room_type == ERoomType.ROOM_TYPE_COIN then
			if not table_data.balanced and request.money.coin ~= seat.diamond_base then
				filelog.sys_error("reentertable: seat info =>", request.money, seat.diamond_base)
				local diff = seat.diamond_base - request.money.coin
				seat.diamond = seat.diamond - diff
				seat.getdiamond = seat.getdiamond - diff
				seat.diamond_base = request.money.coin
				seat.playerinfo.diamond = request.money.diamond
				seat.playerinfo.coin = request.money.coin

			end
		end

		if seat.is_tuoguan == EBOOL.TRUE then
			seat.is_tuoguan = EBOOL.FALSE
			seat.timeout_fold = 0
			seat.is_disconnect = nil
		end
	end

	if seat == nil then
		responsemsg.retcode = EErrCode.ERR_INVALID_REQUEST
		responsemsg.retstr = "无效的请求！"
		return responsemsg
	end
	seat.waitreenter = nil
	responsemsg.gameinfo = {}
	msghelper:copy_table_gameinfo(responsemsg.gameinfo)
	msghelper:sendmsg_to_tableplayer(seat, "reentertableR", responsemsg)

	tablelogic.reentertable(table_data, request, seat)	 
    -- return responsemsg
end

function TableRequest.leavetable(request)
	local responsemsg = {
		-- retcode = EErrCode.ERR_SUCCESS, 
	}
	table_data.standing[request.rid] = nil
	local seat = tablelogic.get_seat_by_rid(table_data, request.rid)
	local isrobot
	if seat then
		if table_data.conf.room_type == ERoomType.ROOM_TYPE_ZJ_FRIEND then
			if table_data.enterclose then
				responsemsg.retcode = EErrCode.ERR_INVALID_REQUEST
				responsemsg.retstr = "游戏已经开始，不能退出！"
				return responsemsg
			end
		else
			if seat.state == ESeatState.SEAT_STATE_WATCHING then

			elseif seat.state == ESeatState.SEAT_STATE_COMPARE_FAIL or seat.state == ESeatState.SEAT_STATE_FOLD then
				if table_data.conf.room_type == ERoomType.ROOM_TYPE_DIAMOND
				or table_data.conf.room_type == ERoomType.ROOM_TYPE_COIN
				or table_data.conf.room_type == ERoomType.ROOM_TYPE_ZJ_DIAMOND then
					if table_data.balanced then
						responsemsg.retcode = EErrCode.ERR_GAMING
						responsemsg.retstr = "游戏中，不能退出!"
						return responsemsg
					end
				end
			else
				if table_data.conf.room_type == ERoomType.ROOM_TYPE_DIAMOND
				or table_data.conf.room_type == ERoomType.ROOM_TYPE_COIN
				or table_data.conf.room_type == ERoomType.ROOM_TYPE_ZJ_DIAMOND then
					if not tablelogic.is_onegameend(table_data) then
						responsemsg.retcode = EErrCode.ERR_GAMING
						responsemsg.retstr = "游戏中，不能退出!"
						return responsemsg
					end
				end
			end
		end
		isrobot = seat.isrobot and 1 or (seat.istest and 2 or nil)
	end
	tablelogic.standuptable(table_data, request, seat)
	tablelogic.leavetable(table_data, request, isrobot)
	return responsemsg
end

function TableRequest.sitdowntable(request)
 	local responsemsg = {
		-- retcode = EErrCode.ERR_SUCCESS, 
	}

	local seat = tablelogic.get_seat_by_rid(table_data, request.rid)
	if seat ~= nil then
		seat.playerinfo.rolename = request.playerinfo.rolename
		seat.playerinfo.logo = request.playerinfo.logo
		seat.playerinfo.sex = request.playerinfo.sex
		return responsemsg
	else
		seat = tablelogic.get_emptyseat_by_index(table_data)
		if seat == nil then
			if table_data.standing[request.rid] then
				tablelogic.leavetable(table_data, request)
			end
			responsemsg.retcode = EErrCode.ERR_NO_EMPTY_SEAT
			responsemsg.retstr = "当前桌子没有空座位了！"
			return responsemsg	
		end
	end
	tablelogic.sitdowntable(table_data, request, seat)
    return responsemsg
end

function TableRequest.standuptable(request)
	local responsemsg = {
		-- retcode = EErrCode.ERR_SUCCESS, 
	}

	local seat = tablelogic.get_seat_by_rid(table_data, request.rid)
	if seat == nil then
		responsemsg.retcode = EErrCode.ERR_HAD_STANDUP
		responsemsg.retstr = "你已经站起了！"
		return responsemsg
	end
	tablelogic.standuptable(table_data, request, seat)
	return responsemsg
end

function TableRequest.gameready(request)
	local responsemsg = {
		-- retcode = EErrCode.ERR_SUCCESS, 
	}

	if server.will_kill then
		for _, seat in ipairs(table_data.seats) do
			if seat.state ~= ESeatState.SEAT_STATE_NO_PLAYER then
				if seat.isrobot then
					tablelogic.passive_standuptable(table_data, seat)
				end
			end
		end

		responsemsg.retcode = EErrCode.ERR_INVALID_REQUEST
		responsemsg.retstr = "服务器即将维护，请退出房间！"
		return responsemsg
	end

	if table_data.state ~= ETableState.TABLE_STATE_WAIT_MIN_PLAYER then
		responsemsg.retcode = EErrCode.ERR_INVALID_REQUEST
		responsemsg.retstr = "无效请求！桌子不是等待准备状态。"
		-- filelog.sys_error("TableRequest.gameready: 无效请求！桌子不是等待准备状态。")
		return responsemsg
	end

	if table_data.conf.room_type == ERoomType.ROOM_TYPE_ZJ_FRIEND then
		if table_data.cur_games_times >= table_data.conf.game_times then
			responsemsg.retcode = EErrCode.ERR_GAME_TIMES_OVER
			responsemsg.retstr = "局数已用完，请重新创建房间！"
			return responsemsg
		end
	elseif table_data.conf.room_type == ERoomType.ROOM_TYPE_ZJ_DIAMOND then
		if table_data.cur_games_times >= table_data.conf.game_times then
			responsemsg.retcode = EErrCode.ERR_GAME_TIMES_OVER
			responsemsg.retstr = "局数已用完，请重新创建房间！"
			return responsemsg
		end
	end

	local seat = tablelogic.get_seat_by_rid(table_data, request.rid)
	if seat == nil then
		responsemsg.retcode = EErrCode.ERR_HAD_STANDUP
		responsemsg.retstr = "玩家不在座位上！"
		filelog.sys_error("TableRequest.gameready: 玩家不在座位上！")
		return responsemsg
	end
	if seat.state ~= ESeatState.SEAT_STATE_WAIT_READY then
		responsemsg.retcode = EErrCode.ERR_HAD_STANDUP
		responsemsg.retstr = "座位状态不是等待准备状态！"
		filelog.sys_error("TableRequest.gameready: 座位状态不是等待准备状态！", seat.state)
		return responsemsg
	end

	if table_data.conf.room_type == ERoomType.ROOM_TYPE_DIAMOND or table_data.conf.room_type == ERoomType.ROOM_TYPE_ZJ_DIAMOND then
		if seat.diamond < table_data.conf.leave_carry_diamond then
			responsemsg.retcode = EErrCode.ERR_NOT_ENOUGH_DIAMOND
			responsemsg.retstr = "您的余额不足，不能继续游戏！"
			filelog.sys_error("TableRequest.gameready: 钻石不够了，先充值吧！")
			return responsemsg
		end
	elseif table_data.conf.room_type == ERoomType.ROOM_TYPE_COIN then
		if seat.diamond < 10 or (seat.diamond + seat.playerinfo.diamond < table_data.conf.leave_carry_diamond) then
			responsemsg.retcode = EErrCode.ERR_NOT_ENOUGH_DIAMOND
			responsemsg.retstr = "您的金币不足，请前往充值！"
			filelog.sys_error("TableRequest.gameready: 砖石不够了，先充值吧！", seat.diamond, table_data.conf.leave_carry_diamond)
			return responsemsg
		end
	end

	responsemsg.state = ESeatState.SEAT_STATE_WAIT_START
	tablelogic.gameready(table_data, request, seat)
	return responsemsg
end

function TableRequest.startgame(request)
	local responsemsg = {
		-- retcode = EErrCode.ERR_SUCCESS, 
	}

	if table_data.state ~= ETableState.TABLE_STATE_WAIT_MIN_PLAYER then
		responsemsg.retcode = EErrCode.ERR_INVALID_REQUEST
		responsemsg.retstr = "无效请求！"
		return responsemsg
	end
	
	if not tablelogic.startgame(table_data, request, true) then
		responsemsg.retcode = EErrCode.ERR_PLAYER_NOTENOUGH
		responsemsg.retstr = "最小人数不足"
		return responsemsg
	end
	return responsemsg
end

local function check_money(table_data, seat, value)
	if table_data.conf.room_type == ERoomType.ROOM_TYPE_DIAMOND then
		return seat.diamond >= value
	elseif table_data.conf.room_type == ERoomType.ROOM_TYPE_ZJ_DIAMOND then
		return seat.diamond >= value
	elseif table_data.conf.room_type == ERoomType.ROOM_TYPE_COIN then
		-- return seat.diamond + seat.playerinfo.diamond >= value
		return seat.diamond >= value
	end
end

function TableRequest.doaction(request)
	-- filelog.debug("doaction:", request)
	local responsemsg = {}
	local seat = tablelogic.get_seat_by_rid(table_data, request.rid)
	if not seat then
		responsemsg.retcode = EErrCode.ERR_HAD_STANDUP
		responsemsg.retstr = "玩家不在座位上！"
		filelog.sys_error("玩家不在座位上！")
		return responsemsg
	end

	if tablelogic.is_onegameend(table_data) then
		responsemsg.retcode = EErrCode.ERR_INVALID_REQUEST
		responsemsg.retstr = "无效请求！游戏已经结束！"
		filelog.sys_error("无效请求！游戏已经结束！")
		return responsemsg
	end

	if table_data.forcecomparing then
		responsemsg.retcode = EErrCode.ERR_INVALID_REQUEST
		responsemsg.retstr = "全桌比牌中，不能操作！"
		filelog.sys_error("全桌比牌中，不能操作！")
		return responsemsg
	end

	if request.action_type < EActionType.ACTION_TYPE_SEECARDS then
		if table_data.state ~= ETableState.TABLE_STATE_WAIT_CLIENT_ACTION
		or seat.index ~= table_data.seat_index then
			responsemsg.retcode = EErrCode.ERR_INVALID_REQUEST
			responsemsg.retstr = "现在不能操作！"
			filelog.sys_error("现在不能操作！")
			return responsemsg
		end
	end

	if not gamelogic.is_ingame(seat) then --是否在游戏，包含弃牌等状态
		responsemsg.retcode = EErrCode.ERR_INVALID_REQUEST
		responsemsg.retstr = "无效请求！没有在游戏中！"
		filelog.sys_error("无效请求！没有在游戏中！")
		return responsemsg
	end

	-----------------------------------非当前操作玩家的看牌,自动跟注，弃牌-----------------------------------
	if request.action_type == EActionType.ACTION_TYPE_SEECARDS then
		if seat.seecards > 0 then
			responsemsg.retcode = EErrCode.ERR_INVALID_REQUEST
			responsemsg.retstr = "无效玩家操作请求,已经看过牌了！"
			return responsemsg
		end
		if table_data.conf.game_type > 2 then
			if seat.state ~= ESeatState.SEAT_STATE_COMPARE_FAIL and seat.state ~= ESeatState.SEAT_STATE_FOLD then
				if table_data.turns < 3 then
					responsemsg.retcode = EErrCode.ERR_INVALID_REQUEST
					responsemsg.retstr = "无效玩家操作请求,必闷三圈才能看牌！"
					return responsemsg
				end
			end
		end
		tablelogic.doaction(table_data, request, seat)
		table_data.robotdata.seecardscount = table_data.robotdata.seecardscount + 1
		return responsemsg
	elseif request.action_type == EActionType.ACTION_TYPE_FOLD then
		if seat.state == ESeatState.ACTION_TYPE_FOLD or seat.state == ESeatState.SEAT_STATE_COMPARE_FAIL then
			responsemsg.retcode = EErrCode.ERR_INVALID_REQUEST
			responsemsg.retstr = "无效玩家操作请求,已经弃牌了！"
			return responsemsg
		end
		tablelogic.doaction(table_data, request, seat)
		return responsemsg
	end
	-----------------------------------当前玩家其他操作-----------------------------------

	if request.action_type == EActionType.ACTION_TYPE_CALL or request.action_type == EActionType.ACTION_TYPE_RAISE then
		if request.action_param <= 0 then
			responsemsg.retcode = EErrCode.ERR_INVALID_REQUEST
			responsemsg.retstr = "无效参数！"
			return responsemsg
		end

		if table_data.conf.room_type == ERoomType.ROOM_TYPE_ZJ_FRIEND then
			if request.action_type == EActionType.ACTION_TYPE_RAISE  then
				if seat.seecards > 0 then
					-- 这里只验证数据，超过上限直接报错，具体数值由客户端处理
					if table_data.conf.base_score*request.action_param*2 > table_data.conf.max_score then
						responsemsg.retcode = EErrCode.ERR_INVALID_PARAMS
						responsemsg.retstr = "押注数量超过了上限！"
						return responsemsg
					end
				else
					if table_data.conf.base_score*request.action_param > table_data.conf.max_score then
						responsemsg.retcode = EErrCode.ERR_INVALID_PARAMS
						responsemsg.retstr = "押注数量超过了上限！"
						return responsemsg
					end
				end
			end
		else
			if request.action_type == EActionType.ACTION_TYPE_CALL then
				if seat.seecards > 0 then
					if not check_money(table_data, seat, table_data.cur_bets*2) then
						responsemsg.retcode = EErrCode.ERR_NOT_ENOUGH_DIAMOND
						responsemsg.retstr = "您的余额不足，不能继续游戏！"
						return responsemsg
					end
				else
					if not check_money(table_data, seat, table_data.cur_bets) then
						responsemsg.retcode = EErrCode.ERR_NOT_ENOUGH_DIAMOND
						responsemsg.retstr = "您的余额不足，不能继续游戏！"
						return responsemsg
					end
				end
			elseif request.action_type == EActionType.ACTION_TYPE_RAISE then
				if seat.seecards > 0 then
					-- 这里只验证数据，超过上限直接报错，具体数值由客户端处理
					if table_data.conf.base_score*request.action_param*2 > table_data.conf.max_score then
						responsemsg.retcode = EErrCode.ERR_INVALID_PARAMS
						responsemsg.retstr = "押注数量超过了上限！"
						return responsemsg
					end

					if not check_money(table_data, seat, table_data.conf.base_score*request.action_param*2) then
						responsemsg.retcode = EErrCode.ERR_NOT_ENOUGH_DIAMOND
						responsemsg.retstr = "您的余额不足，不能继续游戏！"
						return responsemsg
					end
				else
					if table_data.conf.base_score*request.action_param > table_data.conf.max_score then
						responsemsg.retcode = EErrCode.ERR_INVALID_PARAMS
						responsemsg.retstr = "押注数量超过了上限！"
						return responsemsg
					end

					if not check_money(table_data, seat, table_data.conf.base_score*request.action_param) then
						responsemsg.retcode = EErrCode.ERR_NOT_ENOUGH_DIAMOND
						responsemsg.retstr = "您的余额不足，不能继续游戏！"
						return responsemsg
					end
				end
			end
		end
	-------------------------------------玩家比牌-----------------------------------------
	elseif request.action_type == EActionType.ACTION_TYPE_COMPARE then
		if request.action_param <= 0 then
			responsemsg.retcode = EErrCode.ERR_INVALID_REQUEST
			responsemsg.retstr = "无效参数！"
			return responsemsg
		end
		if seat.index == request.action_param then
			responsemsg.retcode = EErrCode.ERR_INVALID_REQUEST
			responsemsg.retstr = "无效参数！不能跟自己比牌"
			return responsemsg
		end
		local comparseat = table_data.seats[request.action_param]
		if gamelogic.is_playing(comparseat) == false then
			responsemsg.retcode = EErrCode.ERR_INVALID_REQUEST
			responsemsg.retstr = "无效参数！"
			return responsemsg
		end

		if table_data.conf.game_type > 2 then
			if table_data.turns < 3 then
				responsemsg.retcode = EErrCode.ERR_INVALID_REQUEST
				responsemsg.retstr = "无效玩家操作请求,必闷三圈才能比牌！"
				return responsemsg
			end
		end
		if table_data.conf.room_type ~= ERoomType.ROOM_TYPE_ZJ_FRIEND then
			local needbets = table_data.cur_bets
			if seat.seecards > 0 then --看牌玩家加倍
				needbets = needbets * 2 
			end
			if not check_money(table_data, seat, needbets) then
				responsemsg.retcode = EErrCode.ERR_NOT_ENOUGH_DIAMOND
				responsemsg.retstr = "您的余额不足，不能继续游戏！"
				return responsemsg
			end
		end
	end
	tablelogic.doaction(table_data, request, seat)
	return responsemsg
end

function TableRequest.remindermoney(request)
	local responsemsg = {}
	local seat = tablelogic.get_seat_by_rid(table_data, request.rid)
	if seat == nil then
		responsemsg.retcode = EErrCode.ERR_HAD_STANDUP
		responsemsg.retstr = "玩家不在座位上！"
		return responsemsg
	end
	if table_data.conf.room_type ~= ERoomType.ROOM_TYPE_DIAMOND and table_data.conf.room_type ~= ERoomType.ROOM_TYPE_ZJ_DIAMOND then
		responsemsg.retcode = EErrCode.ERR_INVALID_REQUEST
		responsemsg.retstr = "该场次不能请求！"
		return responsemsg
	end
	local nowts = timetool.get_time()
	if nowts < (seat.remindermoneyCD or 0) then
		responsemsg.retcode = EErrCode.ERR_INVALID_REQUEST
		responsemsg.retstr = "请求太频繁，请稍候再试！"
		return responsemsg
	end
	msghelper:sendmsg_to_alltableplayer("moneychangeN", {
		money_change_list = {
			{
				rid = seat.rid,
				money_type = 2,
				value = seat.diamond,
			},
		}
	})
	seat.remindermoneyCD = nowts + 3
	return responsemsg
end

function TableRequest.sendtablemessage(request)
	-- body
	local responsemsg = {
		-- retcode = EErrCode.ERR_SUCCESS,
	}
	local seat = tablelogic.get_seat_by_rid(table_data, request.rid)
	if seat == nil  then
		responsemsg.retcode = EErrCode.ERR_NOT_INTABLE
		responsemsg.retstr = "你已经不在桌内！"
		return responsemsg
	end
	------向房间内的玩家广播消息
	local messageresponmsg = {
		rid = seat.rid,
		seat_index = seat.index,
		messages = request.messages,
		chat_type = request.chat_type,
	}
	msghelper:sendmsg_to_alltableplayer("PlayerTableMessageNtc",messageresponmsg)
    return responsemsg
end

--[[
//获取彩池相关信息 
message getjackoptinfo {
	
}

message getjackoptinfoR {
	optional int32 jackopt_num = 1; // 彩池总金额
	optional int32 money_type = 2;
	repeated getjackopt getjackopts = 3; // 瓜分比例和牌型
	repeated getjackoptrecord getjackoptrecords = 4; // 奖池瓜分详情
}
--]]
---玩家点击彩池图标获取彩池信息
function TableRequest.getjackoptinfo(request)
	if true then return end
	local responsemsg = {}

	if table_data.conf.room_type == ERoomType.ROOM_TYPE_COIN then
		responsemsg.retcode = EErrCode.ERR_INVALID_REQUEST
		responsemsg.retstr = "金币场没有彩池"
		return responsemsg
	end

	if table_data.conf.base_score <= 10 then
		return responsemsg
	end

	local status, result = skynet.pcall(skynet.call, ".jackpotmgr", "lua", "getjackInfo", table_data.conf.base_score)
	if not status or next(result) == nil then
		responsemsg.retcode = EErrCode.ERR_INVALID_REQUEST
		responsemsg.retstr = "获取彩池信息失败"
		return responsemsg
	end
	
    return result
end

function TableRequest.disbandtable(request)
	local responsemsg = {
		-- retcode = EErrCode.ERR_SUCCESS,
	}

	if table_data.conf.room_type ~= ERoomType.ROOM_TYPE_ZJ_FRIEND then
		responsemsg.retcode = EErrCode.ERR_INVALID_REQUEST
		responsemsg.retstr = "不是自建桌,不能解散"
		return responsemsg
	end

	if table_data.vote_timerid then
		responsemsg.retcode = EErrCode.ERR_INVALID_REQUEST
		responsemsg.retstr = "已经有人提起投票了"
		return responsemsg
	end

	local seat = tablelogic.get_seat_by_rid(table_data, request.rid)
	if seat == nil then
		responsemsg.retcode = EErrCode.ERR_HAD_STANDUP
		responsemsg.retstr = "玩家不在座位上！"
		return responsemsg
	end

	local vote_wait_time = 40
	local vote_to_time = timetool.get_time() + vote_wait_time
	local votentcmsg = {
		-- rid = request.rid,
		-- name = request.playerinfo.rolename,
		-- wait_to_time = vote_to_time,
		rid = request.rid,
		seat_index = seat.index,
		action_timeout_time = vote_to_time,
		apply_player_name = request.playerinfo.rolename,
		player_disband_list = {},
	}

	for _, seat in ipairs(table_data.seats) do
		if seat.state ~= ESeatState.SEAT_STATE_NO_PLAYER and seat.rid > 0 then
			if seat.rid == request.rid then
				table.insert(votentcmsg.player_disband_list, {
					rid = seat.rid,
					rolename = seat.playerinfo.rolename,
					is_agree = 1,
				})
			else
				table.insert(votentcmsg.player_disband_list, {
					rid = seat.rid,
					rolename = seat.playerinfo.rolename,
				})
			end
		end
	end
	table_data.vote_rid = request.rid
	table_data.vote_name = request.playerinfo.rolename
	table_data.vote_timeout = vote_to_time
	msghelper:sendmsg_to_alltableplayer("disbandtableN", votentcmsg)
	table_data.vote_timerid = timer.settimer(vote_wait_time*100, "disbandtable")
	TableRequest.disbanddeal({rid = request.rid, playerinfo = request.playerinfo, is_agree = 1}, true)
	return responsemsg
end

function TableRequest.disbanddeal(request, nonotice)
	local responsemsg = {
		-- retcode = EErrCode.ERR_SUCCESS,
	}

	local seat = tablelogic.get_seat_by_rid(table_data, request.rid)
	if seat == nil then
		responsemsg.retcode = EErrCode.ERR_INVALID_REQUEST
		responsemsg.retstr = "不是游戏玩家,不能投票!"
		return responsemsg
	end

	if table_data.conf.room_type ~= ERoomType.ROOM_TYPE_ZJ_FRIEND then
		responsemsg.retcode = EErrCode.ERR_INVALID_REQUEST
		responsemsg.retstr = "不是自建桌,不能投票!"
		return responsemsg
	end

	if not table_data.vote_timerid then
		responsemsg.retcode = EErrCode.ERR_INVALID_REQUEST
		responsemsg.retstr = "投票已经结束!"
		return responsemsg
	end

	if table_data.vote_answers and table_data.vote_answers[seat.index] then
		responsemsg.retcode = EErrCode.ERR_INVALID_REQUEST
		responsemsg.retstr = "已经投过票了!"
		return responsemsg
	end

	if not request.is_agree or request.is_agree == 1 then
		table_data.vote_answers = table_data.vote_answers or {}
		table_data.vote_answers[seat.index] = 1
	else
		table_data.vote_num = (table_data.vote_num or 0) + 1	-- 记录不同意的数量，方便结果处理
		-- table_data.vote_names = table_data.vote_names or {}
		-- table.insert(table_data.vote_names, request.playerinfo.rolename)

		table_data.vote_answers = table_data.vote_answers or {}
		table_data.vote_answers[seat.index] = 2
	end

	if not nonotice then
		-- local agree = 0
		-- for _ in pairs(table_data.vote_answers or {}) do
		-- 	agree = agree + 1
		-- end
		local answerntcmsg = {
			rid = request.rid,
			-- agree_num = agree,
			rolename = request.playerinfo.rolename,
			-- is_agree = request.is_agree,
			player_disband_list = {},
		}

		for _, _seat in ipairs(table_data.seats) do
			if _seat.state ~= ESeatState.SEAT_STATE_NO_PLAYER and _seat.rid > 0 then
				table.insert(answerntcmsg.player_disband_list, {
					rid = _seat.rid,
					rolename = _seat.playerinfo.rolename,
					is_agree = table_data.vote_answers[_seat.index],
				})
			end
		end

		msghelper:sendmsg_to_alltableplayer("disbanddealN", answerntcmsg)
		-- msghelper:sendmsg_to_alltableplayer("answerdisbandtableN", answerntcmsg)
	end
	-- local pnum = table_data.conf.max_player_num
	-- if table_data.conf.game_type == EGameType.GAME_TYPE_NORMAL_FOR2 or table_data.conf.game_type == EGameType.GAME_TYPE_CARD15_FOR2 then
	-- 	pnum = table_data.conf.max_player_num - 1
	-- end
	local pnum = table_data.sitdown_player_num
	if (table_data.vote_num and table_data.vote_num > 0) or (table_data.vote_answers and #table_data.vote_answers == pnum) then
		-- local noteresultntcmsg = {
		-- 	result = 0,
		-- 	names = table_data.vote_names,
		-- }

		local noteresultntcmsg = {
			table_index = table_data.conf.idx,
			is_success = false,
		}
		if not table_data.vote_num or table_data.vote_num == 0 then
			noteresultntcmsg.is_success = true
		end
		msghelper:sendmsg_to_alltableplayer("disbandresultN", noteresultntcmsg)
		timer.cleartimer(table_data.vote_timerid)
		table_data.vote_timerid = nil
		table_data.vote_rid = nil
		table_data.vote_name = nil
		table_data.vote_timeout = nil
		table_data.vote_num = nil
		table_data.vote_answers = nil
		-- table_data.vote_names = nil

		if noteresultntcmsg.is_success then
			if table_data.timer_id and table_data.timer_id > 0 then
				timer.cleartimer(table_data.timer_id)
				table_data.timer_id = -1
			end
			for _, seat in ipairs(table_data.seats) do
				if seat.state ~= ESeatState.SEAT_STATE_NO_PLAYER then
					tablelogic.passive_standuptable(table_data, seat, EStandupReason.STANDUP_REASON_DELETE_TABLE)
				end
			end
			-- server.delete(table_data)
			table_data.delete_table_timer_id = timer.settimer(0, "delete_table")
		end
	end
	return responsemsg
end

return TableRequest